//
//  SleepAidViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/28.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import RxSwift
import SwiftFontName

class SleepAidViewController: SleepBaseViewController {
    var tableView: UITableView!

    let bag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        addTableView()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        FetchMusicViewModel()
            .fetch(disposeBag: bag) { [weak self] obj in
                self?.bindUI(obj)
        }
    }

    private func bindUI(_ model: SleepAidMusic) {
        Observable.just(model.sleep_musics)
            .bind(to: tableView.rx.items(cellIdentifier: "cell",
                                         cellType: SleepAidTableViewCell.self)) { _, element, cell in
                cell.titleLabel.text = element.name
        }.disposed(by: bag)
    }

    private func addTableView() {
        let tableView = UITableView(frame: view.bounds, style: .plain)
        view.addSubview(tableView)
        tableView.backgroundColor = .clear
        tableView.tableHeaderView = SleepAidTableHeaderView(frame: CGRect(
            origin: .zero,
            size: CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)))
        tableView.tableFooterView = UIView()
        tableView.register(SleepAidTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.separatorStyle = .none
        tableView.rowHeight = 46
        self.tableView = tableView
    }
}

class SleepAidTableViewCell: UITableViewCell {
    var thumbnailImageView: UIImageView!
    var titleLabel: UILabel!
    var downloadButton: UIButton!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUp() {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = UIImage(named: "icon_paly_small")
        contentView.addSubview(imageView)
        self.thumbnailImageView = imageView
        NSLayoutConstraint.activate([
            imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 16),
            imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            ])

        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont(name: FontName.PingFangSCRegular, size: 16)
        label.textColor = UIColor("#1BBAD1")
        contentView.addSubview(label)
        self.titleLabel = label
        NSLayoutConstraint.activate([
            label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            label.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10),
            ])

        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_download"), for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(button)
        self.downloadButton = button
        NSLayoutConstraint.activate([
            button.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            button.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -16)
            ])

        let separatorLine = UIView()
        separatorLine.backgroundColor = UIColor("#205870")
        separatorLine.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(separatorLine)
        NSLayoutConstraint.activate([
            separatorLine.leftAnchor.constraint(equalTo: contentView.leftAnchor),
            separatorLine.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            separatorLine.heightAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale),
            separatorLine.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
            ])
    }
}

class SleepAidTableHeaderView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUp() {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = bounds.width * 0.2
        imageView.image = UIImage(named: "pic_bg_login")
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor("#052726").cgColor
        imageView.layer.borderWidth = 6
        imageView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(imageView)
        NSLayoutConstraint.activate([
            imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
            imageView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.4),
            imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1),
            imageView.centerYAnchor.constraint(equalTo: centerYAnchor)
            ])

        let prevButton = UIButton(type: .custom)
        prevButton.setImage(UIImage(named: "icon_paly_precious"), for: .normal)
        prevButton.translatesAutoresizingMaskIntoConstraints = false
        addSubview(prevButton)
        NSLayoutConstraint.activate([
            prevButton.centerYAnchor.constraint(equalTo: imageView.centerYAnchor),
            NSLayoutConstraint(item: prevButton, attribute: .centerX, relatedBy: .equal,
                               toItem: self, attribute: .centerX, multiplier: 0.36, constant: 0)
            ])

        let nextButton = UIButton(type: .custom)
        nextButton.setImage(UIImage(named: "icon_paly_next"), for: .normal)
        nextButton.translatesAutoresizingMaskIntoConstraints = false
        addSubview(nextButton)
        NSLayoutConstraint.activate([
            nextButton.centerYAnchor.constraint(equalTo: imageView.centerYAnchor),
            NSLayoutConstraint(item: nextButton, attribute: .centerX, relatedBy: .equal,
                               toItem: self, attribute: .centerX, multiplier: 1.64, constant: 0)
            ])

        let songNameLabel = UILabel()
        songNameLabel.text = "雨巷"
        songNameLabel.textColor = .white
        songNameLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 16)
        songNameLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(songNameLabel)
        NSLayoutConstraint.activate([
            songNameLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
            NSLayoutConstraint(item: songNameLabel, attribute: .centerY, relatedBy: .equal,
                               toItem: self, attribute: .centerY, multiplier: 1.66, constant: 0)
            ])

        let separatorLine = UIView()
        separatorLine.backgroundColor = UIColor("#205870")
        separatorLine.translatesAutoresizingMaskIntoConstraints = false
        addSubview(separatorLine)
        NSLayoutConstraint.activate([
            separatorLine.leftAnchor.constraint(equalTo: leftAnchor),
            separatorLine.rightAnchor.constraint(equalTo: rightAnchor),
            separatorLine.heightAnchor.constraint(equalToConstant: 1 / UIScreen.main.scale),
            separatorLine.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])
    }
}
